<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="部门id" prop="deptId">-->
<!--        <el-input-->
<!--          v-model="queryParams.deptId"-->
<!--          placeholder="请输入部门id"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="用户id" prop="userId">-->
<!--        <el-input-->
<!--          v-model="queryParams.userId"-->
<!--          placeholder="请输入用户id"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="链接地址" prop="link">
        <el-input
          v-model="queryParams.link"
          placeholder="请输入链接地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="二维码参数" prop="qrCodeData">-->
<!--        <el-input-->
<!--          v-model="queryParams.qrCodeData"-->
<!--          placeholder="请输入二维码参数"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="微信已关注账号" prop="account">-->
<!--        <el-input-->
<!--          v-model="queryParams.account"-->
<!--          placeholder="请输入微信已关注账号"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          @click="handleAdd"
          v-hasPermi="['media:mpQrcodeLink:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['media:mpQrcodeLink:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['media:mpQrcodeLink:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['media:mpQrcodeLink:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="mpQrcodeLinkList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键" align="center" prop="id" v-if="true"/>-->
<!--      <el-table-column label="部门id" align="center" prop="deptId" />-->
<!--      <el-table-column label="用户id" align="center" prop="userId" />-->
      <el-table-column label="链接地址" align="center" prop="link" />
      <el-table-column label="二维码数量" align="center" prop="linkNum" >
        <template slot-scope="scope">
          <router-link :to="'/media/mpQrcodeData/index/' + scope.row.id" class="link-type">
            <span>{{ scope.row.linkNum }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="解析图片地址" align="center" prop="imageUrl" >
        <template slot-scope="scope">
          <span >{{scope.row.imageUrl ? scope.row.imageUrl.substr(0,20) + "********" : ""}}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="二维码参数" align="center" prop="qrCodeData" />-->
<!--      <el-table-column label="微信已关注账号" align="center" prop="account" />-->
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="getImgData(scope.row)"
            v-hasPermi="['media:mpQrcodeLink:list']"
          >一键解析链接</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            @click="uploadGif(scope.row)"-->
<!--            v-hasPermi="['media:mpQrcodeLink:edit']"-->
<!--          >上传二维码</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['media:mpQrcodeLink:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['media:mpQrcodeLink:remove']"-->
<!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

<!--    <el-dialog title="二维码页面内容" :visible.sync="openDetail" width="500px" append-to-body>-->
<!--       <div id="qrCodePage"></div>-->
<!--    </el-dialog>-->

    <el-dialog title="二维码上传" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        :headers="upload.headers"
        :action="upload.url + '?id=' + checkId"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 添加或修改微信群二维码参数对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="链接地址" prop="link">
          <el-input v-model="form.link" type="textarea" placeholder="请输入内容" />
        </el-form-item>
<!--        <el-form-item label="二维码参数" prop="qrCodeData">-->
<!--          <el-input v-model="form.qrCodeData" placeholder="请输入二维码参数" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="微信已关注账号" prop="account">-->
<!--          <el-input v-model="form.account" placeholder="请输入微信已关注账号" />-->
<!--        </el-form-item>-->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMpQrcodeLink, getMpQrcodeLink, delMpQrcodeLink, addMpQrcodeLink, updateMpQrcodeLink } from "@/api/media/mpQrcodeLink";
import {getContentDataApi} from "../../../api/media/mpQrcodeLink";
import {Message} from "element-ui";
import { getToken } from "@/utils/auth";

export default {
  name: "MpQrcodeLink",
  data() {
    return {
      qrCodeImg:'',
      checkId:'',
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url:  process.env.VUE_APP_BASE_API + "/media/mpQrcodeLink/upload", // 上传的图片服务器地址
      },
      openDetail:false,
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 微信群二维码参数表格数据
      mpQrcodeLinkList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: undefined,
        userId: undefined,
        link: undefined,
        qrCodeData: undefined,
        account: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "主键不能为空", trigger: "blur" }
        ],
        link: [
          { required: true, message: "链接地址不能为空", trigger: "blur" }
        ],
        // qrCodeData: [
        //   { required: true, message: "二维码参数不能为空", trigger: "blur" }
        // ],
        // account: [
        //   { required: true, message: "微信已关注账号不能为空", trigger: "blur" }
        // ],
        // remark: [
        //   { required: true, message: "备注不能为空", trigger: "blur" }
        // ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询微信群二维码参数列表 */
    getList() {
      this.loading = true;
      listMpQrcodeLink(this.queryParams).then(response => {
        this.mpQrcodeLinkList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },


    /**
     * 上传图片文件
     * @param row
     */
    uploadGif(row){
      this.upload.open = true;
      this.checkId = row.id
    },

    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },

    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      console.log(response)
      this.$alert(response.msg, "上传结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },


    /**
     * 获取二维码内容
     */
    getImgData(rowId){
      this.loading = true;
      getContentDataApi(rowId.id).then((res)=>{
         if(!res.msg){
           Message.warning("链接已失效或不是二维码页!")
         }else{
           Message.success("解析成功！已保存,图片链接为："+res.msg)
         }
      this.getList();
      this.loading = false;
      }).finally(()=>{
        this.loading = false;
      })
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        deptId: undefined,
        userId: undefined,
        createTime: undefined,
        createBy: undefined,
        updateTime: undefined,
        updateBy: undefined,
        link: undefined,
        qrCodeData: undefined,
        account: undefined,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加微信群二维码参数";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getMpQrcodeLink(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改微信群二维码参数";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateMpQrcodeLink(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addMpQrcodeLink(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除微信群二维码参数编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delMpQrcodeLink(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('media/mpQrcodeLink/export', {
        ...this.queryParams
      }, `mpQrcodeLink_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
